package com.tp2.etna.ejemplo.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.tp2.etna.ejemplo.modelo.Empleado;


@Repository
public class EmpleadoDAOImpl implements EmpleadoDAO {
 
	@Autowired
    private DataSource dataSource;
 
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

	@Override
	public void save(Empleado empleado) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Empleado getById(int id) {
        String query = "select * from Empleado where id = ?";
        Empleado emp = null;
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            con = dataSource.getConnection();
            ps = con.prepareStatement(query);
            ps.setInt(1, id);
            rs = ps.executeQuery();
            if(rs.next()){
                emp = new Empleado();
                emp.setId(id);
                emp.setNombre(rs.getString("nombre"));
                emp.setId(rs.getInt("id"));
                emp.setDireccion(rs.getString("direccion"));
                emp.setApellidoPaterno(rs.getString("apellido_paterno"));
                emp.setApellidoMaterno(rs.getString("apellido_materno"));
                System.out.println("Empleado Found::"+emp);
            }else{
                System.out.println("No Employee found with id="+id);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            try {
                rs.close();
                ps.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return emp;
	}

	@Override
	public void update(Empleado employee) {
		// TODO Auto-generated method stub
		String query = "update Empleado set nombre=?, apellido_paterno=?, apellido_materno = ?, direccion = ? where id=?";
        Connection con = null;
        PreparedStatement ps = null;
        try{
            con = dataSource.getConnection();
            ps = con.prepareStatement(query);
            ps.setString(1, employee.getNombre());
            ps.setString(2, employee.getApellidoPaterno());
            ps.setString(3, employee.getApellidoMaterno());
            ps.setString(4, employee.getDireccion());
            ps.setInt(5, employee.getId());
            int out = ps.executeUpdate();
            if(out !=0){
                System.out.println("Empleado encontrado="+employee.getId());
            }else System.out.println("Empleado no encontrado con id="+employee.getId());
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            try {
                ps.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
	}

	@Override
	public void deleteById(int id) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<Empleado> getAll() {
		// TODO Auto-generated method stub
		return null;
	}
 
}